//
//  AppreciationListViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppreciationListViewController.h"
#import "AppercitionTCell.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperationManager.h"
#import "AppreciationDetailViewController.h"
#import "AppreciationModel.h"
#import "UIImageView+WebCache.h"
#import "UIScrollView+MJRefresh.h"
#import "NightManager.h"
@interface AppreciationListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) UITableView *tableView;
@property(nonatomic, assign) NSInteger number;
@property(nonatomic, retain) NSMutableArray *storyArr;
@end

@implementation AppreciationListViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc
{
    [_modelArr release];
    [_tableView release];
    [_nTitle release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.nTitle;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64- 49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    [self addHeader];
    [self addFooter];

}

- (void)addHeader
{
#pragma mark 列表头
    [self.storyArr removeAllObjects];
    [self.tableView reloadData];
    [self.tableView addHeaderWithCallback:^{
        self.number = 0;

        [self getData];
    }];
    [self.tableView headerBeginRefreshing];
}

- (void)addFooter
{
#pragma mark 列表脚
    [self.tableView addFooterWithCallback:^{
        self.number++;
        [self getData];
    }];
}

- (void)getData
{
#pragma mark 数据解析
    
    
    NSString *strURL = [NSString stringWithFormat:@"http://img.trends.com.cn/tmen/apphd/%ld/%ld.jsn?&mid=FFFFFFFF9F1A6654BBEB45838159645D254FADAB&code=77D15D6D9802A29AD6BFC8EE646E3CA2", self.webID, self.number];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arrFirst = [responseObject objectForKey:@"articles"];
        for (NSDictionary *dic in arrFirst) {
            AppreciationModel *model = [[AppreciationModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            NSDictionary *dic2 = [dic objectForKey:@"coverPhoto2"];
            model.url = [dic2 objectForKey:@"url"];
            model.webID = self.webID;
            [self.modelArr addObject:model];
            [model release];
        }
        
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

#pragma mark cell的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}

#pragma mark 生成cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    AppercitionTCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[AppercitionTCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    AppreciationModel *model = self.modelArr[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.timeLabel.text = model.time;
    cell.anotherLabel.text = model.author;
    NSURL *strUrl = [NSURL URLWithString:model.url];
    [cell.photoView sd_setImageWithURL:strUrl placeholderImage:[UIImage imageNamed:@"aishang"]];
    return cell;
}

#pragma mark 点击切换页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppreciationDetailViewController *ADVC = [[AppreciationDetailViewController alloc]init];
    ADVC.mobel = self.modelArr[indexPath.row];
    [self.navigationController pushViewController:ADVC animated:YES];
    [ADVC release];
}

#pragma mark cell高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.tableView];
    
}

- (void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.tableView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
